//
//  MainViewController.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/9.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置tabbar
        let tabbar = TabBar()
        tabbar.tabDelegate = self
        tabbar.composeButtonClosure = { [weak self] in
            print("从闭包过来")
        }
        setValue(tabbar, forKey: "tabBar")
        //因为只读  所以不能这么写
//        self.tabBar = TabBar()
        //添加子控制器
        addChildViewController(HomeTableViewController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }

    //设置子控制器的属性
    func addChildViewController(childController: UIViewController , title: String, imageName: String) {
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
        
    }

}
extension MainViewController: TabBarDelegate {
    func didselectedComposeButton() {
        print("从代理过来")
    }
}